package hermesV2.services.P2P_Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;




import hermesV2.Message;
import hermesV2.PlaceAddress;
import hermesV2.agent.*;

public class PeerAgent extends UserAgent {
  

/**
	 * 
	 */
	private static final long serialVersionUID = 3737497582715219329L;
/**
	 * 
	 */
/**
	 * 
	 */
	private Vector<PlaceAddress> peers;
	private  Vector<String> peers_online;
    private  Vector<Date> peers_eventTime;

public PeerAgent(String agentName) {
        super("PeerAgent");
  }

  
  public void init() {
    reception(); //I enable the reception  of messages for the father
    loginToService(getServiceIdentificator("PeerToPeer_v1.0"), "ezio", "ezio");
    Message<?> m = this.getMessageSynch();      // wait to know where it should go
    if (m != null){
    	if (m.getObject() instanceof PlaceAddress){
    	   PlaceAddress peer = (PlaceAddress)m.getObject();
    	   try {
    		  this.move(peer, "movedToAnotherPeer"); //movedToAnotherPeer is the method to execute just arrived
    	   } catch (MigrationException me) {

    		 System.out.println("---PeerAgent MigrationException ");
    		 me.printStackTrace();
    		  this.dead();
    	   }
    	}
    }
  }
  
  public void movedToAnotherPeer(){
	  reception(); 
	  PlaceAddress peerPlaceToRegister = this.getIdentificator().getBornPlaceAddress();
	 System.out.println(  "---peerAgent just born moved here from "+peerPlaceToRegister.getIp());
	  
	  /*Find the peer to peer Service Identiifcator*/
	  Identificator serviceId = getServiceIdentificator("PeerToPeer_v1.0");
	  
	  if (serviceId != null){
		  InteractionContainer ic = new InteractionContainer(getServiceProtocol(serviceId));
		  Object [] params = new Object[3];
		  params[0] = peerPlaceToRegister;
		  params[1] = "true";
		  params[2] = Calendar.getInstance().getTime();
		  ic.makeInteraction("AddIP", params);
		  try {
			loginToService(serviceId, "ezio", "ezio");
			System.out.println( "---try to add" + peerPlaceToRegister.toString());
			sendMessageToServiceAgent(new Message<InteractionContainer>(getIdentificator(), serviceId, ic));
			Message<?> response = getMessageSynch();
			System.out.println("---response = " + ((InteractionContainer)response.getObject()).getParameter("Response")+". now i ask for the updated peers list");
			ic.makeInteraction("AskPeersList", "");
			sendMessageToServiceAgent(new Message<InteractionContainer>(getIdentificator(), serviceId, ic));
			response = getMessageSynch();
			peers = (Vector<PlaceAddress>) ((InteractionContainer)response.getObject()).getParameter("PeersList");
			peers_online = (Vector<String>) ((InteractionContainer)response.getObject()).getParameter("AreOnline");
			peers_eventTime = (Vector<java.util.Date>) ((InteractionContainer)response.getObject()).getParameter("EventTime");
			System.out.println("---peers list updated");
		  } catch (CommunicationException e) { System.out.println(  "---Error "+e);}
	  }
	  else {System.out.println("service agent "+serviceId.getName()+" not found");}
	  // COME BACK TO THE FATHER
	  try {
		 System.out.println( "---coming back");
		  this.move(peerPlaceToRegister, "comingBackToThePeer");
	   } catch (MigrationException me) {
		  me.printStackTrace();
		 System.out.println( "---MigrationException" + me);
		  this.dead();
	   }
  }
  

  public void comingBackToThePeer (){
	  reception(); // enable reception of message (i register the agent in the mobility service agent)
	  loginToService(getServiceIdentificator("PeerToPeer_v1.0"), "ezio", "ezio"); // i enable this agent to communicate with the service agent PeerToPeer_v1.0
	 System.out.println( "---came back! try to update peers list ;)");
	  Identificator serviceId = getServiceIdentificator("PeerToPeer_v1.0");
	  InteractionContainer ic = new InteractionContainer(getServiceProtocol(serviceId));
	  Object [] params = new Object[3];
	  params[0] = peers;
	  params[1] = peers_online;
	  params[2] = peers_eventTime;
	  ic.makeInteraction("UpdatePeersList", params);
  	  try {
		   sendMessageToServiceAgent(new Message<InteractionContainer>(getIdentificator(), serviceId, ic));
		   Message<?> response = getMessageSynch();
		   System.out.println( "---response = " + ((InteractionContainer)response.getObject()).getParameter("Response"));
	      } 
  	  catch (CommunicationException e) { System.out.println(  "---Error "+e);}

	  this.dead();
  }
  
}
